package com.lty.jeeg.server.modules.fas.dao;

import java.util.List;

import com.lty.jeeg.server.modules.fas.dto.GoodsCategoryDTO;
import org.apache.ibatis.annotations.*;

import org.springframework.stereotype.Repository;

import com.lty.jeeg.server.modules.fas.model.GoodsCategory;

/**
 * @author : LTY
 * @date : 2019-01-18 09:17:16
 * Description : 商品分类
 */
@Mapper
@Repository
public interface GoodsCategoryDao {

    /**
     * 根据ID获取
     * @param id ID
     * @return bean
     */
    @Select("select " +
            "t.*, t2.name parentName " +
            "from fas_goods_category t " +
            "left join fas_goods_category t2 on t.parentId = t2.id " +
            "where t.isDeleted = 0 AND t.id = #{id}")
    GoodsCategoryDTO getById(Long id);

    /**
     * 根据名称查询
     * @param categoryName
     * @return
     */
    @Select("select " +
            "t.*, t2.name parentName " +
            "from fas_goods_category t " +
            "left join fas_goods_category t2 on t.parentId = t2.id " +
            "where t.isDeleted = 0 AND t.name = #{categoryName}")
    GoodsCategoryDTO getByName(String categoryName);

    /**
     * 获取上级
     * @param id ID
     * @return 上级bean
     */
    GoodsCategoryDTO getParentById(Long id);

    /**
     * 删除（系统级别表格看情况使用delete，业务表一律使用update）
     * @param id ID
     * @return 1/0
     */
    @Update("update fas_goods_category set isDeleted = 1 where id = #{id}")
    int delete(Long id);

    /**
     * 修改
     * @param goodsCategory ID
     * @return 1/0
     */
    int update(GoodsCategory goodsCategory);

    /**
     * 保存
     * @param goodsCategory bean
     * @return ID
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into fas_goods_category(name, parentId, parentIds, usable, remark, createBy, updateBy, isDeleted, sort) values(#{name}, #{parentId}, #{parentIds}, #{usable}, #{remark}, #{createBy}, #{updateBy}, #{isDeleted}, #{sort})")
    Long save(GoodsCategory goodsCategory);

    /**
     * 树状列表
     * for 列表页面
     * @return 列表
     */
    @Select("select t.* from fas_goods_category t where isDeleted = 0 order by t.sort")
    List<GoodsCategoryDTO> treeList();

    /**
     * 树状列表
     * for Ztree
     * @return 所有数据
     */
    @Select("select t.* from fas_goods_category t where isDeleted = 0 and usable = 1 order by t.sort")
    List<GoodsCategoryDTO> listAll();

}
